<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '财务审核管理',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

<!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(3)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <div class="widget-title  am-cf">财务审核管理</div>
                        </div>
                        <div class="widget-body  am-fr">
                            <div class="am-u-sm-12 am-u-md-6 am-u-lg-6 am-form-group">
                                <div class="am-btn-toolbar">
                                    <div class="am-alert am-alert-danger" id="data-alert" style="display: none;margin: 4px 0 10px 10px;font-size: 1.6rem;">
                                        <p>开始日期应小于结束日期</p>
                                    </div>
                                    <div class="am-g">
                                        <div class="am-u-sm-6">
                                            <button type="button" class="am-btn am-btn-default am-margin-right" id="my-start">开始日期</button><span id="my-startDate"></span>
                                        </div>
                                        <div class="am-u-sm-6">
                                            <button type="button" class="am-btn am-btn-default am-margin-right" id="my-end">结束日期</button><span id="my-endDate"></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                <?php

                                    if($_GET['type'] == 1){
                                        echo '<div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: \'sm\',btnWidth: \'100%\'}" id="getStatus">
                                            <option value="null">请选择状态</option>
                                            <option value="0">待确定</option>
                                            <option value="1">已确定</option>
                                        </select>
                                    </div>
                                </div>';   
                                    }

                                ?>
                                

                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                    <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
    <!--                                    <form action="/admin/user/search" method="get">-->
                                        <input type="text" class="am-form-field" name="search" id="searchValue" placeholder="请输入申请单名称">
                                        <span class="am-input-group-btn">
                                            <a href="javascript:void(0);" id="search" class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                        </span>
    <!--                                    </form>-->
                                    </div>
                                </div>
                                
                            </div>
                            <div class="am-u-sm-12">
                                <div class="am-btn-toolbar am-u-sm-12">
                                    <div class=" am-btn-group-xs" id="type">
<!--                                        <a href="javascript:;" class="am-btn am-btn-primary ">进货单</a>-->
<!--                                        <a href="javascript:;" class="am-btn am-btn-primary ">已审核项目</a>-->

                                        <a href="javascript:void(0);" onclick="dai()" class="am-btn   <?php if(isset($_GET['type']) && $_GET['type'] == 1){echo 'am-btn-primary';}  else {echo 'am-btn-default';}?> ">进货单</a>
                                        <a href="javascript:void(0);" onclick="yi()" class="am-btn  <?php if(isset($_GET['type']) && $_GET['type'] == 2){echo 'am-btn-primary ';} else {echo 'am-btn-default';}?> ">抄送项目单</a>

                                        <!-- <a href="javascript:void(0);" onclick="dai()" class="am-btn am-btn-primary ">进货单</a> -->
                                        <!-- <a href="javascript:void(0);" onclick="yi()" class="am-btn am-btn-primary">招投标评分</a> -->
                                    </div>
                                </div>
                            </div>
                            <div class="am-u-sm-12">
                                <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black "
                                       id="example-r">
                                    <thead>
                                    <tr>
                                        <?php
                                            if(isset($list)){
                                                echo '<th>单据编号</th>
                                        <th>单据类型</th>
                                        <th>单据名称</th>
                                        <th>发起时间</th>
                                        <th>所属项目</th>
                                        <th>种类/数量</th>
                                        <th>预付款比例</th>
                                        <th>状态</th>
                                        <th>操作</th>';
                                            }else{
                                                echo '<th>单据编号</th>
                                        <th>单据类型</th>
                                        <th>单据名称</th>
                                        <th>发起时间</th>
                                        <th>所属项目</th>
                                        <th>种类/数量</th>
                                        <th>状态</th>
                                        <th>操作</th>';
                                            }
                                        ?>

                                    </tr>
                                    </thead>
                                    <tbody class="choose-box">
                                    <?php
                                    if(isset($list) && $list){
                                        foreach ($list[0] as $v) {
                                            echo '<tr class="gradeX">';
                                            echo '<td><a href="/admin/procurement/purchaseDetail?id='.$v['id'].'&status='.$v['onStatus'].'">'.$v['purchaseCode'].'</a></td>';
                                             echo '<td>'.$v['type'].'</td>
                                             <td>'.$v['name'].'</td>
                                             <td>'.date('Y-m-d',$v['createTime']).'</td>
                                            <td>'.$v['source'].'</td>
                                            <td>'.$v['attribute'].'</td>
                                            <td>'.$v['nowPrepay'].'</td>';

                                             if($v['status'] == 1){
                                                 echo '<td>待确定</td>
                                                            <td>
                                                                <div class="tpl-table-black-operation">
                                                                    <a href="/admin/goldCheck/purchaseConfirmPay?id='.$v['id'].'&status='.$v['status'].'&onStatus='.$v['onStatus'].'&prepay='.$v['nowPrepay'].'">
                                                                        <i class="am-icon-check"></i> 确定付款
                                                                    </a>
                                                                </div>
                                                            </td>
                                                            </tr>';
                                             }else{
                                                 echo '<td>已确定</td>
                                                            <td>
                                                                <div class="tpl-table-black-operation">
                                                              
                                                                </div>
                                                            </td>
                                                            </tr>';
                                             }
                                        }
                                    }else{
                                        if(isset($list2) && $list2){
                                            foreach ($list2[0] as $v) {
                                                echo '<tr class="gradeX">';
                                                switch ($v['type']){
                                                    case '普通需求单':
                                                        echo '<td><a href="/admin/procurement/editView?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                    break;
                                                    case '招投标项目需求单':
                                                        echo '<td><a href="/admin/procurement/editView?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                    case '采购项目单':
                                                        echo '<td><a href="/admin/procurement/proProjectEditView?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                    case '采购退货单':
                                                        echo '<td><a href="/admin/procurement/returnOrderEditView?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                    case '采购换货单':
                                                        echo '<td><a href="/admin/procurement/barterOrderEditView?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                    case '商品出库单':
                                                        echo '<td><a href="/admin/procurement/outDetail?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                    case '商品入库单':
                                                        echo '<td><a href="/admin/procurement/putDetail?id='.$v['relevanceId'].'">'.$v['code'].'</a></td>';
                                                        break;
                                                }


//                                                if($v['type'] == '普通需求单' || $v['type'] == '招投标项目需求单'){
//                                                    echo '<td><a href="/admin/procurement/editView?id='.$v['id'].'">'.$v['code'].'</a></td>';
//                                                }else{
//                                                    echo '<td><a href="/admin/procurement/proProjectEditView?id='.$v['id'].'">'.$v['code'].'</a></td>';
//                                                }
                                                echo '<td>'.$v['type'].'</td>
                                             <td>'.$v['name'].'</td>
                                             <td>'.date('Y-m-d',$v['createTime']).'</td>
                                            <td>'.$v['source'].'</td>
                                            <td>'.$v['attribute'].'</td>';

                                                if($v['status'] == 1){
                                                    echo '<td>已确定</td>
                                                            <td>
                                                                <div class="tpl-table-black-operation">
                                                              
                                                                </div>
                                                            </td>
                                                            </tr>';
                                                }
                                            }

                                        }
                                    }
                                    ?>
                                    </tbody>
                                </table>
                            </div>
                            <div class="am-u-lg-12">
                                <div class="am-u-sm-centered">
                                    <ul class="am-pagination am-pagination-centered" id="paging">

                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<script>
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
    function dai() {
        var search = GetQueryString('search');

        if(search == null){
            window.location.href='?page=1'+'&type=1'
        }else{
            window.location.href='?page=1'+'&search='+search+'&type=1'
        }
    }
    function yi() {
        var search = GetQueryString('search');
        if(search == null){
            window.location.href='?page=1'+'&type=2'
        }else{
            window.location.href='?page=1'+'&search='+search+'&type=2'

        }
    }
</script>

<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>

<script>
    //筛选
    var startTime,
        endTime,
        status,
        searchValue,
        page,
        type;

    $("#getStatus").on( "change" , function() {
        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        status = $("#getStatus").val();
        searchValue = $("#searchValue").val();
        page = 1;
        type = getType();
        
        chooseGoldCheck(startTime,endTime,status,searchValue,page,type);

    });
    $("#search").on( "click" , function() {
        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        status = $("#getStatus").val();
        searchValue = $("#searchValue").val();
        page = 1;
        type = getType();
        
        chooseGoldCheck(startTime,endTime,status,searchValue,page,type);
    });

    //筛选日期
    var startDate = new Date(2014, 11, 20);
    var endDate = new Date(2100, 11, 25);
    var $alert = $('#data-alert');
    $('#my-start').datepicker().on('changeDate.datepicker.amui', function(event) {
        
        if (event.date.valueOf() > endDate.valueOf()) {
          $alert.find('p').text('开始日期应小于结束日期！').end().show();
          return false;
          
        } else {
          $alert.hide();
          startDate = new Date(event.date);
          $('#my-startDate').text($('#my-start').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        status = $("#getStatus").val();
        searchValue = $("#searchValue").val();
        page = 1;
        type = getType();
        
        chooseGoldCheck(startTime,endTime,status,searchValue,page,type);
        
    });
    $('#my-end').datepicker().on('changeDate.datepicker.amui', function(event) {
        
        if (event.date.valueOf() < startDate.valueOf()) {
          $alert.find('p').text('结束日期应大于开始日期！').end().show();
          return false;
        } else {
          $alert.hide();
          endDate = new Date(event.date);
          $('#my-endDate').text($('#my-end').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        status = $("#getStatus").val();
        searchValue = $("#searchValue").val();
        page = 1;
        type = getType();

        chooseGoldCheck(startTime,endTime,status,searchValue,page,type);
       
    });

    function getType() {
        var type;
        var typeA = $("#type").children("a.am-btn-primary");
        if(typeA.text() == "进货单"){
            return type = 1;
        }else{
            return type = 2;
        }
    }

    function chooseGoldCheck(startTime,endTime,status,searchValue,page,type){
        $.ajax({
            type: "GET",
            url : "/api/goldCheck/search",
            data: {
                startTime: startTime,
                endTime: endTime,
                status: status,
                search: searchValue,
                page: page,
                type: type
            },
            dataType: "json",
            beforeSend:function(){
                $(".my-dark").css("display","block");
            },
            success: function(msg){
                console.log(msg)
                var data = msg['0'];
                var list;       
                console.log(data);

                if(msg.type == 1){

                    var listStatus,
                        go; //操作
                    if(!data[0]){
                        list += '<tr class="gradeX"><td colspan="9">暂无数据</td></tr>';
                    }else{
                        for(var i = 0 ; i < data.length;i++ ){
                            listStatus = data[i].status == 1 ? '待确定' : '已确定';
                            go =  data[i].status == 1 ? '<div class="tpl-table-black-operation"><a href="/admin/goldCheck/purchaseConfirmPay?id='+ data[i].id +'&status='+ data[i].status +'&onStatus='+ data[i].onStatus +'"><i class="am-icon-check"></i> 确定付款</a></div>' : '';

                            list += '<tr class="gradeX">'
                                    + '<td>'
                                        + '<a href="/admin/procurement/purchaseDetail?id='+ data[i].id +'&status='+ data[i].onStatus +'">'+ data[i].purchaseCode +'</a>'
                                    + '</td>'
                                    + '<td>'+ data[i].type +'</td>'
                                    + '<td>'+ data[i].name +'</td>'
                                    + '<td>'+ data[i].createTime +'</td>'
                                    + '<td>'+ data[i].source +'</td>'
                                    + '<td>'+ data[i].attribute +'</td>'
                                    + '<td>'+ data[i].nowPrepay +'</td>'
                                    + '<td>'+ listStatus +'</td>'
                                    + '<td>'+ go +'</td>'
                                 + '</tr>';
                        }

                    }
                    $(".choose-box").html(list);

                    //分页
                    //设置分页信息  
                    var pageOptions = {  
                        AllowPaging: true,  
                        PageIndex: msg.this_page,      //设置当前页码  
                        PageSize: msg.pageNum,       //设置分页大小  
                        RecordCount: msg.total_num,  //设置数据总数  
                        // TotalPage: ,      //设置总页数
                        showPageCount: 4,  
                        onPageClick: function(pageIndex) {
                            chooseGoldCheck(startTime,endTime,status,searchValue,pageIndex,type);
                        }  
                    }  
                    //初始化分页栏  
                    $('#paging').pageBar(pageOptions); 

                }else{

                    var code;
                    if(!data[0]){
                        list += '<tr class="gradeX"><td colspan="8">暂无数据</td></tr>';
                    }else{
                        for(var i = 0 ; i < data.length;i++ ){
                            if(data[i].type == '普通需求单' || data[i].type == '招投标项目需求单'){
                                code = '<a href="/admin/procurement/editView?id='+ data[i].id +'">'+ data[i].code +'</a>';
                            }else{
                                code = '<a href="/admin/procurement/editView?id='+ data[i].id +'">'+ data[i].code +'</a>';
                            }

                            list += '<tr class="gradeX">'
                                    + '<td>'+ code +'</td>'
                                    + '<td>'+ data[i].type +'</td>'
                                    + '<td>'+ data[i].name +'</td>'
                                    + '<td>'+ data[i].createTime +'</td>'
                                    + '<td>'+ data[i].source +'</td>'
                                    + '<td>'+ data[i].attribute +'</td>'
                                    + '<td>已确定</td>'
                                    + '<td></td>'
                                 + '</tr>';
                        }

                    }
                    $(".choose-box").html(list);

                    //分页
                    //设置分页信息  
                    var pageOptions = {  
                        AllowPaging: true,  
                        PageIndex: msg.this_page,      //设置当前页码  
                        PageSize: msg.pageNum,       //设置分页大小  
                        RecordCount: msg.total_num,  //设置数据总数  
                        // TotalPage: ,      //设置总页数
                        showPageCount: 4,  
                        onPageClick: function(pageIndex) {
                            chooseGoldCheck(startTime,endTime,status,searchValue,pageIndex,type);
                        }  
                    }  
                    //初始化分页栏  
                    $('#paging').pageBar(pageOptions);

                }
                

            },
            complete:function(){
                $(".my-dark").css("display","none");
            },
            error: function (data) {  
               //  alert(XMLHttpRequest.status);
               //  alert(XMLHttpRequest.readyState);
               //  alert(textStatus);
            }
        });
    }
</script>

<script>
    $(document).ready(function() {  
        //设置分页信息  
        var pageOptions = {  
            AllowPaging: true,  
            PageIndex: <?php echo $_GET['page'] ?>,      //设置当前页码  
            PageSize: <?php echo $pageNum ?>,       //设置分页大小  
            RecordCount: <?php echo $totalNum ?>,  //设置数据总数  
            // TotalPage: ,      //设置总页数
            showPageCount: 4,  
            onPageClick: function(pageIndex) {  
                var search = '<?php echo isset($_GET['search'])  ? $_GET['search'] : 0 ?>';
                var status = '<?php echo isset($_GET['status'])  ? $_GET['status'] : 0 ?>';
                var type = '<?php echo isset($_GET['type'])  ? $_GET['type'] : 1 ?>';
                
                var page = parseInt(pageIndex);

                if(search){
                    window.location.href = "?page="+ page +"&search="+ search +"&status=" + status  + "&type=" + type;
                }else {
                    window.location.href = "?page="+ page +"&status=" + status + "&type=" + type;
                }
                
            }  
        }  
        //初始化分页栏  
        $('#paging').pageBar(pageOptions);      
    })  
</script>

</body>

</html>